package com.lyw.home

import androidx.activity.viewModels
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import coil.load
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ScreenUtils
import com.lu.library.base.BaseActivity
import com.lyw.home.databinding.ActivityPoetyDetailBinding
import com.lyw.home.vm.PoetryDetailVm
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import me.shouheng.utils.ktx.onDebouncedClick

@Route(path = HomeRouterPaths.HOME_POETRY_DETAIL)
@AndroidEntryPoint
class PoetryDetailActivity :BaseActivity<ActivityPoetyDetailBinding>(){


    @JvmField
    @Autowired
    var id:Int=0

    private val poetryDetailVm by viewModels<PoetryDetailVm>()
    override fun initData() {
        lifecycleScope.launch {
            poetryDetailVm.uiStateFlow.collect{
                binding.tvAuthor.text = it.author
                binding.tvContent.text = it.content
                binding.tvPoetyTitle.text = it.title
                binding.ivAvatar.load(it.avatar)

                binding.tvRemark.text = it.remark
                binding.tvAdmire.text = it.admire
                if (it.like) {
                    binding.titleBar.rightIcon = getDrawable(R.mipmap.home_like)
                }else{
                    binding.titleBar.rightIcon = getDrawable(R.mipmap.home_unlike)
                }
            }
        }
        poetryDetailVm.init(id)

    }

    override fun initListener() {
        super.initListener()

        binding.titleBar.rightView.onDebouncedClick {
            poetryDetailVm.toggleLike()
        }
    }
    override fun initViews() {
        super.initViews()
        binding.nestedScrollView.updatePadding(bottom = BarUtils.getNavBarHeight())
    }

    override fun layoutResId(): Int = R.layout.activity_poety_detail
}